package level4.display.helpers
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * 显示 对象的助手类
	 * */
	public class DisplayObjectHelper
	{
		/**
		 * 停止MovieClip所有的子对象的play
		 * @param container
		 * @param isStopMySelf
		 * 
		 */		
		public static function stopAllChildren(container:MovieClip,isStopMySelf:Boolean=true):void{
			if(isStopMySelf){
				container.stop();
			}
			for(var i:int=0;i<container.numChildren;i++){
				var child:MovieClip = container.getChildAt(i) as MovieClip;
				if(child){
					child.stop();
					stopAllChildren(child);
				}
			}
		}
		/**
		 * 停留到这帧
		 * @param mc
		 * @param frame
		 * 
		 */		
		public static function gotoAndStopThisFrame(mc:MovieClip,frame:int):void{
			mc.gotoAndStop(frame);
		}
		/**
		 * 禁用鼠标
		 * */
		public static function disableMouse(mc:MovieClip):void{
			mc.mouseChildren = false;
			mc.mouseEnabled = false;
		}
		/**
		 * 让do2d盯住pt
		 * @param do2d
		 * @param pt
		 * 
		 */		
		public static function lookAt(do2d:DisplayObject,pt:Point):void{
			var dx:Number = pt.x - do2d.x;
			var dy:Number = pt.y - do2d.y;
			var angle:Number = Math.atan2(dy,dx);
			do2d.rotation = angle * 180 /Math.PI;
		}

		/**
		 * 将子类移除,并尝试销毁子类的数据
		 * @param do2d
		 * 
		 */		
		public static function removeAllChildren(do2d:DisplayObjectContainer):void{
			if(do2d != null){
				while(do2d.numChildren>0){
					var removed:DisplayObject = do2d.removeChildAt(0);
					//容器
					if(removed is DisplayObjectContainer){
						removeAllChildren(removed as DisplayObjectContainer);
					}
				}
			}
		}
		/**
		 * 只将 子类 移除
		 * @param do2d
		 * 
		 */		
		public static function removeAllChildrenSimple(do2d:DisplayObjectContainer):void{
			if(do2d){
				while(do2d.numChildren >0){
					do2d.removeChildAt(0);
				}
			}
		}
		/**
		 * 移除所有子类的 filter
		 * @param do2d
		 * 
		 */		
		public static function removeAllChildrenFilter(do2d:DisplayObjectContainer):void{
			for(var i:int=0;i<do2d.numChildren;i++){
				do2d.getChildAt(i).filters = [];
			}
			do2d.filters = [];
		}
		/**
		 * 将所有的 子类执行 cacheAsBitmap
		 * @param do2d
		 * @param isCache
		 * 
		 */		
		public static function recursionCacheAsBitmap(do2d:DisplayObjectContainer,isCache:Boolean):void{
			for(var i:int=0;i<do2d.numChildren;i++){
				var c:DisplayObject = do2d.getChildAt(i);
					c.cacheAsBitmap = isCache;
				if(c is DisplayObjectContainer){
					recursionCacheAsBitmap(c as DisplayObjectContainer,isCache);
				}
			}
		}
		
	}
}